package com.gsm.company.controller;

import com.gsm.common.comtroller.BaseController;
import com.gsm.common.entity.Result;
import com.gsm.common.entity.ResultCode;
import com.gsm.company.service.CompanyService;
import com.gsm.company.service.DepartmentService;
import com.gsm.domain.company.Company;
import com.gsm.domain.company.Department;
import com.gsm.domain.company.resp.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: gaoshengmin
 * @CreateDate: 2020/2/8$ 11:59$
 * @UpdateUser: gaoshengmin
 * @UpdateDate: 2020/2/8$ 11:59$
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/company/department")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CompanyService companyService;


    @PostMapping
    public Result save(@RequestBody Department department) {
        department.setCompanyId(companyId);
        departmentService.add(department);
        return new Result(ResultCode.SUCCESS);
    }

    @GetMapping
    public Result findAll() {
        List<Department> all = departmentService.findAll(companyId);
        Result result = new Result(ResultCode.SUCCESS);
        Company company = companyService.findById(companyId);
        DeptListResult deptListResult = new DeptListResult(company, all);
        result.setData(deptListResult);
        return result;
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        Department department = departmentService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(department);
        return result;
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        departmentService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable String id ,  @RequestBody Department department){
        department.setId(id);
        departmentService.update(department);
        return new Result(ResultCode.SUCCESS);
    }

}
